<?php	
    function getLanguageCode_debit()
    {
    	$strlang='en-us	';
    	switch($_SESSION['language'])
        {
        	case 'english':
        	     $strlang='en-us';
        	     break;
        	case 'schinese':
        	     $strlang='zh-cn';
        	     break;
            default:
                 $strlang='en-us';
        }
        return $strlang;
    }
    /**
     * creat process_button string to glbpay
     */
     function glbpay_button_debit($order_id)
    {
    	global $db,$currencies;
    	$sql="insert into " . TABLE_GLBPAYDEBIT_SESSION . "(session_id,orders_id,session_info,session_date) values('" . zen_db_input(session_id()) . "','".$order_id."','".base64_encode(serialize($_SESSION))."', now())";
		$db->Execute($sql);
    	require_once(DIR_WS_CLASSES . 'order.php');
    	$order = new order($order_id);
        $cmd = 'fgpay';
		$version='1.1.0';
        $encoding='utf-8';
        $strlang=getLanguageCode_debit();
        $merchantid=MODULE_PAYMENT_GLBPAYDEBIT_MERCHANTID;
        $orderid=MODULE_PAYMENT_GLBPAYDEBIT_ORDER_PREFIX .str_pad($order_id, 15, '0', STR_PAD_LEFT);
        $datestr=date('YmdHis',strtotime($order->info['date_purchased']));
        $currency=$order->info['currency'];
        $amount = sprintf('%.2f', $order->info['total']* $currencies->get_value($currency));
        $transtype='sale';
        $remark1='';
        $remark2=$order_id;
		
        $strServerUrl=str_replace(".php.html",".php",zen_href_link('glbpay_checkout_debit.php', 'referer=glbpay', 'SSL',false,false,true));
        $strBrowserurl=str_replace(".php.html",".php",zen_href_link('glbpay_checkout_debit.php', 'referer=glbpay', 'SSL',false,false,true));

        $bill_sql= "select countries_iso_code_2, countries_iso_code_3
                          from " . TABLE_COUNTRIES . "
                          where countries_name = '" . $order->billing['country'] . "'";
        $bill_country=$db->Execute($bill_sql);
        $delivery_sql= "select countries_iso_code_2, countries_iso_code_3
                          from " . TABLE_COUNTRIES . "
                          where countries_name = '" . $order->delivery['country'] . "'";
        $delivery_country=$db->Execute($delivery_sql);

        $strProduct = '';
        $strProductdes='';
        $i=0;
        for (; $i<sizeof($order->products)&&$i<50; $i++) {
        	  $tempsn=strcheck($order->products[$i]["id"]);
        	  $tempprice=sprintf('%.2f', $order->products[$i]["price"]* $currencies->get_value($currency));
        	  $tempname=trim($order->products[$i]["name"]);
        	  $strProductdes=$strProductdes.html_entity_decode($tempname,ENT_NOQUOTES,"UTF-8").$tempsn.$order->products[$i]["qty"].$tempprice;
              $strProduct = $strProduct . zen_draw_hidden_field('productname'.($i+1), $tempname) .
                                          zen_draw_hidden_field('productsn'.($i+1), $tempsn) .
                                          zen_draw_hidden_field('quantity'.($i+1), $order->products[$i]["qty"]) .
                                          zen_draw_hidden_field('unit'.($i+1), $tempprice);
        }
		//billing info
        $billcountry=$bill_country->fields['countries_iso_code_3'];
		$billaddress=$order->billing['street_address'];
		$billstate=str_replace(chr(160), '', $order->billing['state']);
		$billcity=$order->billing['city'];
		$billemail=$order->customer['email_address'];
		$billphone=$order->customer['telephone'];
		$firstname = $order->customer['firstname'];
		$lastname = $order->customer['lastname'];
		$billpost=$order->billing['postcode'];
        
        
		//delivery info
        $deliveryname=$order->delivery['name'];
		$deliveryaddress=$order->delivery['street_address'];
		$deliverycountry=$delivery_country->fields['countries_iso_code_3'];
		$deliverystate=str_replace(chr(160), '', $order->delivery['state']);
		$deliverycity=$order->delivery['city'];
        $deliveryemail=$order->customer['email_address'];
        $deliveryphone=$order->customer['telephone'];
        $deliverypost=$order->delivery['postcode'];
        $zencartver='Zen Cart';

        $sqlordertotals="select * from ". TABLE_ORDERS_TOTAL ." where orders_id= '".$order_id."'";
        $ordertotal=$db->Execute($sqlordertotals);
        $shipfee='0.00';
        while(!$ordertotal->EOF)
        {        	
        	if($ordertotal->fields['class']=='ot_shipping')
        	{
        		$shipfee=sprintf('%.2f', $ordertotal->fields['value']* $currencies->get_value($currency));
				break;
        	}
        	$ordertotal->MoveNext();
        }
        //$value=$cmd.$version.$encoding.$strlang.$merchantid .$orderid . $datestr . $currency .
               //$amount . $strServerUrl .$strBrowserurl. $remark1.$remark2.$remark3. $strProductdes .
               //$shipfee . $billaddress .$billcountry .$billstate.$billcity.$billemail.$billphone.
               //$billpost  . $deliveryaddress . $deliverycountry .$deliverystate.
               //$deliverycity . $deliveryemail. $deliveryphone . $deliverypost;
			   
		 $dataArr = array(
		 'version' => $version,
		 'language' => $strlang,
		 'merchantid' => MODULE_PAYMENT_GLBPAYDEBIT_MERCHANTID,
		 'orderid' => $orderid,
		 'orderdate' => $datestr,
		 'ordercurrency' => $currency,
		 'orderamount' => $amount,
		 'browserurl' => $strBrowserurl,
		 'serverurl' => $strServerUrl,
		 'remark1' => $remark1,
		 'remark2' => $remark2,
		 'remark3' => $remark3,
		 'billingfirstname' => $firstname,
		 'billinglastname' => $lastname,
		 'billingaddress' => $billaddress,
		 'billingcountry' => $billcountry,
		 'billingstate' => $billstate,
		 'billingcity' => $billcity,
		 'billingpost' => $billpost,
		 'billingemail' => $billemail,
		 'billingphone' => $billphone,
		 'deliveryfirstname' => $firstname,
		 'deliverylastname' => $lastname,
		 'deliveryaddress' => $deliveryaddress,
		 'deliverycountry' => $deliverycountry,
		 'deliverystate' => $deliverystate,
		 'deliverycity' => $deliverycity,
		 'deliverypost' => $deliverypost,
		 'deliveryemail' => $deliveryemail,
		 'deliveryphone' => $deliveryphone); 
		 
		 ksort($dataArr);
		$value = '';
		foreach($dataArr as $arrKey => $arrVal){
			//echo $arrKey .':'. $arrVal . '<br />';
			$value =  $value . $arrVal;
		}
		//echo $value;
		error_log("Oracle database not available!", 0);
        $hkey=trim(MODULE_PAYMENT_GLBPAYDEBIT_SSLKEY);
	    $process_button_string = zen_draw_hidden_field('cmd', $cmd) .
								 zen_draw_hidden_field('version', $version) .
	                             zen_draw_hidden_field('encoding', $encoding) .
	                             zen_draw_hidden_field('language', $strlang) .
	                             zen_draw_hidden_field('merchantid', MODULE_PAYMENT_GLBPAYDEBIT_MERCHANTID) .
	                             zen_draw_hidden_field('orderid', $orderid) .
	                             zen_draw_hidden_field('orderdate', $datestr) .
	                             zen_draw_hidden_field('ordercurrency', $currency) .
	                             zen_draw_hidden_field('orderamount', $amount) .
	                             zen_draw_hidden_field('serverurl', $strServerUrl) .
	                             zen_draw_hidden_field('browserurl', $strBrowserurl) .
	                             zen_draw_hidden_field('remark1', $remark1) .
	                             zen_draw_hidden_field('remark2', $remark2) .
	                             zen_draw_hidden_field('remark3', $remark3) .$strProduct .
	                             zen_draw_hidden_field('shippingfee', $shipfee) .
								 zen_draw_hidden_field('billingfirstname', $firstname) .
								 zen_draw_hidden_field('billinglastname', $lastname) .
	                             zen_draw_hidden_field('billingaddress', $billaddress) .
	                             zen_draw_hidden_field('billingcountry', $billcountry) .
	                             zen_draw_hidden_field('billingstate',$billstate) .
	                             zen_draw_hidden_field('billingcity', $billcity) .
	                             zen_draw_hidden_field('billingemail', $billemail) .
	                             zen_draw_hidden_field('billingphone', $billphone) .
	                             zen_draw_hidden_field('billingpost', $billpost) .
	                             zen_draw_hidden_field('deliveryfirstname', $firstname) .
								 zen_draw_hidden_field('deliverylastname', $lastname) .
	                             zen_draw_hidden_field('deliveryaddress',$deliveryaddress) .
	                             zen_draw_hidden_field('deliverycountry', $deliverycountry) .
	                             zen_draw_hidden_field('deliverystate',$deliverystate) .
	                             zen_draw_hidden_field('deliverycity', $deliverycity) .
	                             zen_draw_hidden_field('deliveryemail', $deliveryemail) .
	                             zen_draw_hidden_field('deliveryphone', $deliveryphone) .
	                             zen_draw_hidden_field('deliverypost', $deliverypost) .
	                             zen_draw_hidden_field('signature', md5($hkey.$value))
                            ;
							
         return $process_button_string;
    }
    function strcheck($str='')
	{
		if($str!='')
		{
			if(!strpos($str,':'))
			{
				return $str;
			}
			else
			{
				return substr($str,0,strpos($str,':'));
			}
		}
	}
?>